﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.ComponentModel;
using DTO;
using System.Collections.Generic;

namespace TVC_WEB.Global
{
    public partial class prodetail : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cate"] != null)
                {
                    lbCatePath.Text = GetCategoryPath(Session["cate"].ToString());
                }
                LoadProduct();
                LoadImages();
            }
        }
        public string GetIndex(string value)
        {
            return Convert.ToString(Convert.ToInt32(value) + 1);
        }
        private void LoadImages()
        {
            if (Request["id"] != null)
            {
                string path = "/" + ConfigurationSettings.AppSettings["UploadFolder"].ToString() + "Images/";
                string numL = "";
                string imgL = "";
                BUS.ListImageControl ctr = new BUS.ListImageControl();
                DataTable dt = ctr.GetByPro(Convert.ToInt64(Request["id"]), ref sErr);
                lImages.DataSource = dt;
                lImages.DataBind();
                foreach (DataRow row in dt.Rows)
                {
                    numL += "1,";
                    imgL += "'" + path + "large" + row["IMG"].ToString() + "',";
                }
                hdImgList.Value = imgL;
                hdNumList.Value = numL;
            }
        }
        public string GetFirstImg()
        {
            string[] arr = hdImgList.Value.Split(',');
            if (arr.Length > 0 && arr[0] != "")
            {
                return arr[0].Substring(1, arr[0].Length - 2);
            } return "";
        }
        public string GetCategoryPath(string cate)
        {
            string result = "";//"<a href=\"/\">Home</a>&nbsp;&nbsp;";
            string[] arrCare = cate.Split(',');
            BUS.CategoriesControl ct = new BUS.CategoriesControl();
            for (int i = 0; i < arrCare.Length; i++)
            {
                DTO.CategoriesInfo inf = ct.Get(Convert.ToInt32(arrCare[i]), ref sErr);
                if (inf.Type == "Category")
                {
                    DataTable dt = ct.GetAllParents(inf.ID, ref sErr);
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        DataRow row = dt.Rows[j];
                        if (j == 0)
                            result = "<a href='prolist.aspx?cate=" + row["ID"].ToString() + "&catetype=Category'><b>" + row["NameVN"].ToString() + "</b></a>" + result;
                        else
                            result = "<a href='prolist.aspx?cate=" + row["ID"].ToString() + "&catetype=Category'>" + row["NameVN"].ToString() + "</a> &nbsp;&nbsp;&gt;&nbsp;&nbsp;" + result;
                    }
                    return "<a href='/Global/'>Home</a>&nbsp;&nbsp;&gt; &nbsp;&nbsp;" + result;

                }
            }
            return "<a href='/Global/'>Home</a>";
        }
        private void LoadProduct()
        {
            if (Request["id"] != null)
            {
                BUS.ProductsControl proctr = new BUS.ProductsControl();
                DTO.ProductsInfo proInf = proctr.Get(Convert.ToInt64(Request["id"]), ref sErr);
                BUS.CategoriesControl ctctr = new BUS.CategoriesControl();
                SetData(proInf);
                LoadListRelate(ctctr.GetCateByPro(proInf.ID, ref sErr));
            }
        }
        public string GetImage(string imageName)
        {
            string strUploadFolder = "/" + ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            return strUploadFolder + imageName;
        }
        public string GetPrice(string imageName)
        {
            return Convert.ToDouble(imageName).ToString("N0") + "đ";
        }
        private void LoadListRelate(string cate)
        {

            BUS.CategoriesControl ctctr = new BUS.CategoriesControl();
            string arrcate = ctctr.GetAllChilds(cate, ref sErr);
            BUS.ProductsControl proCtr = new BUS.ProductsControl();
            DataTable dt = proCtr.GetByArrCate(arrcate, ref sErr);
            lvPro.DataSource = dt;
            lvPro.DataBind();
        }

        private void SetData(DTO.ProductsInfo proInf)
        {
            BUS.CategoriesControl cateCtr = new BUS.CategoriesControl();
            DataTable dt = cateCtr.GetCateAllByPro(proInf.ID, "Category", ref sErr);
            dt.DefaultView.Sort = "ParentRank,Rank,ParentNameVN,NameVN";
            
            dt.DefaultView.RowFilter = "(not(LinkedID is null))";
            string flag = "";
            string result = "";
            for (int i = 0; i < dt.DefaultView.Count; i++)
            {
                DataRowView row = dt.DefaultView[i];
                if (row["LinkedID"] != DBNull.Value && row["Type"].ToString() != "Price")
                {
                    if (row["LinkedID"].ToString() != flag)
                    {
                        result += "</dl><dl><dt><img src='/Global/Images/list.png' /> " + row["ParentNameVN"].ToString() + "</dt>";
                        flag = row["LinkedID"].ToString();
                    }
                    result += "<dd><img src='/Global/Images/check.png' /> " + row["NameVN"].ToString() + "</dd>";
                }
            }
            result = result.Substring(5) + result.Substring(0, 5);
            imgIcon.ImageUrl = "/" + ConfigurationSettings.AppSettings["UploadFolder"].ToString() + "Images/" + proInf.Icon;
            lbSubCate.Text = result;
            lbName.Text = proInf.NameVN;
            liSortDesc.Text = proInf.ShortDecribesVN;
            liMaterial.Text = proInf.Material;
            liSize.Text = proInf.Dimension;
            lbPrice.Text = proInf.NewPrice.ToString("##,###") + "đ";
            lbDescribe.Text = proInf.LongDecribesVN;
        }

        protected void btnAddBasket_Click(object sender, EventArgs e)
        {
            if (Request["id"] != null)
            {
                if (Session["cart"] == null)
                {
                    List<CartInfo> lcart = new List<CartInfo>();
                    int qty = txtQuantity.Value == null ? 1 : Convert.ToInt32(txtQuantity.Value.Value);
                    lcart.Add(new CartInfo(Convert.ToInt64(Request["id"]), qty));
                    Session["cart"] = lcart;
                }
                else
                {
                    List<CartInfo> lcart = Session["cart"] as List<CartInfo>;
                    bool flag = true;
                    foreach (CartInfo x in lcart)
                    {
                        if (x.ProductID.ToString() == Request["id"].ToString())
                        {
                            flag = false; break;
                        }

                    }
                    if (flag)
                    {
                        int qty = txtQuantity.Value == null ? 1 : Convert.ToInt32(txtQuantity.Value.Value);
                        lcart.Add(new CartInfo(Convert.ToInt64(Request["id"]), qty));
                        Session["cart"] = lcart;
                    }
                }
            }
            Response.Redirect("cart.aspx");
        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            if (Session["currentshop"] != null)
                Response.Redirect(Session["currentshop"].ToString());
        }

        protected void lImages_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
    }
}
